package it.blueocean.acanto.entities.platform;

import it.blueocean.acanto.entities.platform.security.Authority;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Index;

/**
 * @author Fabio Mauro
 *
 */
@Entity
@Table(name = "TB_GROUPS")
@org.hibernate.annotations.Table(appliesTo = "TB_GROUPS", indexes = { @Index(name = "IDX_GROUP_ID", columnNames = { "GROUP_ID" }) })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Group implements Serializable
{
    private static final long serialVersionUID = 549840541L;
    
    @Id
	@Column(name = "GROUP_ID", unique = true, nullable = false, length = 50)
    private String id;
    
    @Column(name = "DESCRIPTION", nullable = true, length = 200)
	private String description;	 	

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
	private Set<Authority> roles = new HashSet<Authority>();
    
//    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
//    private Set<DBUser> users = new HashSet<DBUser>(0);
	
    public Group()
    {
    }
    
    @Override
	public Object clone()
	{
		Group clone = new Group();
		clone.setId(getId());
		clone.setDescription(getDescription());

		return clone;
	}

    public Group(String groupId)
    {
    	this.setId(groupId);
    }
    
	public boolean equals(Object obj)
	{
        if (obj instanceof Group)
        {
        	Group a = (Group) obj;
            return 
                a.getId().equals(id) && 
                a.getDescription().equals(description);
        }
        else
            return false;        	
	}
	
	public String toString()
	{
		return new StringBuffer()
			.append("id : " + getId()).append("\n") 
			.append("description : " + getDescription()).append("\n") 
			.toString();
	}
	
	@Override
    public int hashCode()
    {        
        return getId().hashCode();
    }

	public String getDescription()
	{
		return description;
	}

	public void setDescription(String description)
	{
		this.description = description;
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public Set<Authority> getRoles()
	{
		return roles;
	}

	public void setRoles(Set<Authority> roles)
	{
		this.roles = roles;
	}

//	public Set<DBUser> getUsers()
//	{
//		return users;
//	}
//
//	public void setUsers(Set<DBUser> users)
//	{
//		this.users = users;
//	}

}
